package cz.proks.easyhelpdesk.gae.core.ws;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cz.proks.easyhelpdesk.api.HelpdeskMemberApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.HelpdeskMember;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.gae.core.manager.HelpdeskMemberManager;

@Component(Configuration.Core.WS.HELPDESK_MEMBER_API)
public class HelpdeskMemberApiImpl extends BaseApiImpl<HelpdeskMember> implements HelpdeskMemberApi {
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    private HelpdeskMemberManager manager;
    
    @Override
    protected HelpdeskMemberManager getManager() {
        return manager;
    }

    @Override
    public HelpdeskMember getForUserAndHelpdesk(Long userId, Long helpdeskId) throws ApiException {
        log.debug("getForUserAndHelpdesk():: Start with userId '{}', helpdeskId '{}'", userId, helpdeskId);
        
        HelpdeskMember helpdeskMember = getManager().getForUserAndHelpdesk(userId, helpdeskId);
        
        log.debug("getForUserAndHelpdesk():: End with '{}'", helpdeskMember);
        return helpdeskMember;
    } 
    
    @Override
    public List<HelpdeskMember> listForUser(Long userId) throws ApiException {
        log.debug("listForUser():: Start with userId '{}', helpdesk '{}'", userId);

        List<HelpdeskMember> helpdeskMembers = getManager().listForUser(userId);

        log.debug("listForUser():: End with {}", (helpdeskMembers == null ? "null list" : "list of " + helpdeskMembers.size() + " elements"));
        return helpdeskMembers;
    }
    
    @Override
    public List<HelpdeskMember> listForHelpdesk(Long helpdeskId) throws ApiException {
        log.debug("listForHelpdesk():: Start with heldpeskId '{}'", helpdeskId);

        List<HelpdeskMember> helpdeskMembers = getManager().listForHelpdesk(helpdeskId);

        log.debug("listForHelpdesk():: End with {}", (helpdeskMembers == null ? "null list" : "list of " + helpdeskMembers.size() + " elements"));
        return helpdeskMembers;
    }
    
    @Override
    public void addHelpdeskRole(Long id, HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("addHelpdeskRole():: Start with id '{}', helpdeskRole '{}'", id, helpdeskRole);

        getManager().addHelpdeskRole(id, helpdeskRole);

        log.debug("addHelpdeskRole():: Exit");        
    }

    @Override
    public void removeHelpdeskRole(Long id, HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("removeHelpdeskRole():: Start with id '{}', helpdeskRole '{}'", id, helpdeskRole);

        getManager().removeHelpdeskRole(id, helpdeskRole);

        log.debug("removeHelpdeskRole():: Exit");
    }        
}
